package org.wishwork.api.stock.other.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.api.stock.other.upload.request.Item;
import org.wishwork.api.stock.other.upload.request.OtherInOutUploadRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 新建其它出入库（新）
 * @author: WangYX
 * @create: 2022-04-12 14:02
 * @Version: 1.0.0
 **/

public class OtherInOutUploadApi extends Api<OtherInOutUploadResponse, OtherInOutUploadRequest> {

    private Context context;

    public OtherInOutUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(OtherInOutUploadRequest otherInOutUploadRequest) throws Exception {
        String url = "/open/jushuitan/otherinout/upload";
        Map<String, Object> params = new HashMap<>();
        params.put("is_confirm", otherInOutUploadRequest.getIsConfirm());
        if (otherInOutUploadRequest.getWmsCoId() != null) {
            params.put("wms_co_id", otherInOutUploadRequest.getWmsCoId());
        }
        params.put("type", otherInOutUploadRequest.getType());
        params.put("external_id", otherInOutUploadRequest.getExternalId());
        params.put("warehouse", otherInOutUploadRequest.getWarehouse());
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getRemark())) {
            params.put("remark", otherInOutUploadRequest.getRemark());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverName())) {
            params.put("receiver_name", otherInOutUploadRequest.getReceiverName());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverMobile())) {
            params.put("receiver_mobile", otherInOutUploadRequest.getReceiverMobile());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverState())) {
            params.put("receiver_state", otherInOutUploadRequest.getReceiverState());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverCity())) {
            params.put("receiver_city", otherInOutUploadRequest.getReceiverCity());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverDistrict())) {
            params.put("receiver_district", otherInOutUploadRequest.getReceiverDistrict());
        }
        if (StrUtil.isNotEmpty(otherInOutUploadRequest.getReceiverAddress())) {
            params.put("receiver_address", otherInOutUploadRequest.getReceiverAddress());
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Item item : otherInOutUploadRequest.getItems()) {
            Map<String, Object> map = new HashMap<>();
            map.put("sku_id", item.getSkuId());
            map.put("qty", item.getQty());
            if (StrUtil.isNotEmpty(item.getBatchId())) {
                map.put("batch_id", item.getBatchId());
            }
            if (StrUtil.isNotEmpty(item.getProducedDate())) {
                map.put("produced_date", item.getProducedDate());
            }
            if (StrUtil.isNotEmpty(item.getExpirationDate())) {
                map.put("expiration_date", item.getExpirationDate());
            }
            if (StrUtil.isNotEmpty(item.getRemark())) {
                map.put("remark", item.getRemark());
            }
            if (ObjectUtil.isNotEmpty(item.getSalePrice())) {
                map.put("sale_price", item.getSalePrice());
            }
            list.add(map);
        }
        params.put("items", list);
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected OtherInOutUploadResponse process(JSONObject data, OtherInOutUploadRequest otherInOutUploadRequest) throws Exception {
        return data.toJavaObject(OtherInOutUploadResponse.class);
    }
}
